package cn.tedu._05springaop.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Arrays;
@Order(10)
@Aspect
@Component
public class LogAspect {
    @Pointcut("execution(public int cn.tedu._05springaop.aop.CalculatorImpl.*(..))")
    public void doTime() {}
//    @Before("doTime()")
//    public void before(JoinPoint joinPoint) {
//        String name = joinPoint.getSignature().getName();
//        String args= Arrays.toString(joinPoint.getArgs());
//        System.out.println("[前置通知]："+name+"方法开始，参数为"+args);
//    }
//    @After("doTime()")
//    public void after(JoinPoint joinPoint) {
//        String name = joinPoint.getSignature().getName();
//        System.out.println("[后置通知]："+name+"方法结束");
//    }
//    @AfterReturning(value = "doTime()",returning = "result")
//    public void afterReturning(JoinPoint joinPoint,Object result) {
//        String name = joinPoint.getSignature().getName();
//        System.out.println("[返回通知]："+name+"方法返回值为"+result);
//    }
//    @AfterThrowing(value = "doTime()",throwing = "ex")
//    public void afterThrowing(JoinPoint joinPoint,Throwable ex) {
//        System.out.println("[异常通知]：方法抛出异常"+ex.getMessage());
//    }
    @Around("doTime()")
    public Object around(ProceedingJoinPoint joinPoint){
        Object result=null;
        try {
            System.out.println("[环绕通知]：方法开始");
            result=joinPoint.proceed();
            System.out.println("[环绕通知]：方法结束");
        } catch (Throwable e) {
            System.out.println("[环绕通知]：方法抛出异常");
            throw new RuntimeException(e);
        }finally {
            System.out.println("[环绕通知]：方法结束");
        }
        return result;
    }
}
